package com.ruoyi.cinema.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cinema.domain.SchedulePromoArea;
import com.ruoyi.cinema.domain.SchedulePromoCardInfo;
import com.ruoyi.cinema.mapper.SchedulePromoAreaMapper;
import com.ruoyi.cinema.mapper.SchedulePromoCardInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cinema.mapper.SchedulePromoMapper;
import com.ruoyi.cinema.domain.SchedulePromo;
import com.ruoyi.cinema.service.ISchedulePromoService;

/**
 * 排期优惠Service业务层处理
 *
 * @author HHAOYU
 * @date 2020-12-13
 */
@Service
public class SchedulePromoServiceImpl extends ServiceImpl<SchedulePromoMapper, SchedulePromo> implements ISchedulePromoService  {
    @Autowired
    private SchedulePromoMapper schedulePromoMapper;
    @Autowired
    private SchedulePromoAreaMapper schedulePromoAreaMapper;
    @Autowired
    private SchedulePromoCardInfoMapper schedulePromoCardInfoMapper;

    /**
     * 查询排期优惠
     *
     * @param id 排期优惠ID
     * @return 排期优惠
     */
    @Override
    public SchedulePromo selectSchedulePromoById(Long id)
    {
        return schedulePromoMapper.selectSchedulePromoById(id);
    }

    /**
     * 查询排期优惠列表
     *
     * @param schedulePromo 排期优惠
     * @return 排期优惠
     */
    @Override
    public List<SchedulePromo> selectSchedulePromoList(SchedulePromo schedulePromo)
    {
        return schedulePromoMapper.selectSchedulePromoList(schedulePromo);
    }

    /**
     * 新增排期优惠
     *
     * @param schedulePromo 排期优惠
     * @return 结果
     */
    @Override
    public int insertSchedulePromo(SchedulePromo schedulePromo)
    {
        return schedulePromoMapper.insertSchedulePromo(schedulePromo);
    }

    /**
     * 修改排期优惠
     *
     * @param schedulePromo 排期优惠
     * @return 结果
     */
    @Override
    public int updateSchedulePromo(SchedulePromo schedulePromo)
    {
        return schedulePromoMapper.updateSchedulePromo(schedulePromo);
    }

    /**
     * 批量删除排期优惠
     *
     * @param ids 需要删除的排期优惠ID
     * @return 结果
     */
    @Override
    public int deleteSchedulePromoByIds(Long[] ids)
    {
        return schedulePromoMapper.deleteSchedulePromoByIds(ids);
    }

    /**
     * 删除排期优惠信息
     *
     * @param id 排期优惠ID
     * @return 结果
     */
    @Override
    public int deleteSchedulePromoById(Long id) {
        return schedulePromoMapper.deleteSchedulePromoById(id);
    }

    @Override
    public int deleteSchedulePromoAllByScheduleId(Long filmScheduleId){
        SchedulePromo schedulePromo = new SchedulePromo();
        schedulePromo.setScheduleId(String.valueOf(filmScheduleId));
        List<SchedulePromo> schedulePromoList = selectSchedulePromoList(schedulePromo);
        for (SchedulePromo sp :schedulePromoList) {
            SchedulePromoCardInfo schedulePromoCardInfo = new SchedulePromoCardInfo();
            schedulePromoCardInfo.setSchedulePromoId(sp.getId());
            List<SchedulePromoCardInfo> spciList = schedulePromoCardInfoMapper.selectSchedulePromoCardInfoList(schedulePromoCardInfo);
            schedulePromoCardInfoMapper.deleteSchedulePromoCardInfoByIds(spciList.stream().map(SchedulePromoCardInfo::getId).collect(Collectors.toList()).toArray(new Long[spciList.size()]));
            SchedulePromoArea schedulePromoArea = new SchedulePromoArea();
            schedulePromoArea.setSchedulePromoId(sp.getId());
            List<SchedulePromoArea> spaList = schedulePromoAreaMapper.selectSchedulePromoAreaList(schedulePromoArea);
            schedulePromoAreaMapper.deleteSchedulePromoAreaByIds(spaList.stream().map(SchedulePromoArea::getId).collect(Collectors.toList()).toArray(new Long[spaList.size()]));
        }
       return schedulePromoMapper.deleteSchedulePromoByIds(schedulePromoList.stream().map(SchedulePromo::getId).collect(Collectors.toList()).toArray(new Long[schedulePromoList.size()]));
    }
}
